001    package net.sf.xdc.util;
002    
003    /*
004     *  Copyright 2005-2006 Jens Voß.
005     *
006     *  Licensed under the GNU Lesser General Public License (the "License");
007     *  you may not use this file except in compliance with the License.
008     *  You may obtain a copy of the License at
009     *
010     *       http://opensource.org/licenses/lgpl-license.php
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    import javax.xml.transform.TransformerException;
020    
021    import org.w3c.dom.traversal.NodeIterator;
022    import org.w3c.dom.Node;
023    import org.apache.xpath.XPathAPI;
024    
025    /**
026     * The <code>XPathUtils</code> class contains a few utility
027     * methods used for selecting subnodes from a DOM node by an
028     * XPath expression.
029     *
030     * @author Jens Voß
031     * @since 0.6
032     * @version 0.6
033     */
034    public class XPathUtils {
035    
036      private XPathUtils() {
037      }
038    
039      /**
040       * This method retrieves a <code>NodeIterator</code> which iterates
041       * over all subnodes of a given DOM node satisfying the specified
042       * XPath pattern.
043       *
044       * @param node The DOM node to which the XPath pattern is applied
045       * @param xPath The XPath expression applied to the DOM node
046       * @return A <code>NodeIterator</code> over all of the specified
047       *         DOM node's subnodes satisfying the specified XPath
048       *         expression
049       * @throws TransformerException
050       */
051      public static NodeIterator selectNodes(Node node, String xPath)
052              throws TransformerException {
053        return XPathAPI.selectNodeIterator(node, xPath);
054      }
055    
056      /**
057       * This method retrieves the single subnode of the given DOM
058       * <code>Node</code> which satisfies the specified XPath pattern.
059       *
060       * @param node The DOM node to which the XPath pattern is applied
061       * @param xPath The XPath expression applied to the DOM node
062       * @return The single subnode of the specified DOM node satisfying
063       *         the specified XPath expression
064       * @throws TransformerException
065       */
066      public static Node selectNode(Node node, String xPath) throws
067              TransformerException {
068        return XPathAPI.selectSingleNode(node, xPath);
069      }
070    
071    }